package cn.smbms.controller;

import cn.smbms.beans.Provider;
import cn.smbms.service.Provider.ProviderService;
import com.alibaba.fastjson.JSONArray;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;


@Controller
@RequestMapping("provider")
public class ProviderController {
    @Autowired
    @Qualifier("providerService")
    private  ProviderService  providerService;
    private Logger log = Logger.getLogger(ProviderController.class);

    @RequestMapping(value = "providerList.html",produces = {"text/html;charset=UTF-8;","application/json;"})
    @ResponseBody
    public  String getProviderList(){
        List<Provider> providerList = providerService.getProviderList();
        String s = JSONArray.toJSONString(providerList);
        return s;
    }

    @RequestMapping("provider.html")
    public ModelAndView provider(String queryProCode ,String queryProName ){
        List<Provider> providerList = providerService.getProvider(queryProCode,queryProName);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("providerList",providerList);
        modelAndView.setViewName("providerlist");
        return  modelAndView;
    }
    //添加供应商
    @RequestMapping("addProvider.html")
    public  String addProvider(){
      return  "provideradd";
    }
    //保存添加
    @RequestMapping("doProvider.html")
    public String doProvider(String proCode,String proName,String proContact, String proPhone, String proAddress ,String proDesc ){
        Provider provider = new Provider();
        provider.setProCode(proCode);
        provider.setProName(proName);
        provider.setProContact(proContact);
        provider.setProPhone(proPhone);
        provider.setProAddress(proAddress);
        provider.setProDesc(proDesc);
        int i = providerService.doProvider(provider);
        if (i > 0) {
            return "redirect:/provider/provider.html";
        }else {
            return  "redirect:/provider/addProvider.html";
        }
    }
    //删除供应商
    @RequestMapping("deleteProvider.html")
    @ResponseBody
    public String  deleteProvider(String proid){
        HashMap<String, String> map = new HashMap<>();
        int i = providerService.deleteProvider(proid);
        Integer id =Integer.parseInt(proid);
        if (id < 0 ){
            map.put("delResult","notexist");
        }else {
            if (i > 0 ){
                map.put("delResult","true");
            }else {
                map.put("delResult","false");
            }
        }
        return  JSONArray.toJSONString(map);
    }
    //查看
    @RequestMapping("providerView.html")
    public ModelAndView providerView(String proid){
        Provider provider = providerService.providerView(proid);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("provider",provider);
        modelAndView.setViewName("providerview");
        return modelAndView;
    }
    //修改
    @RequestMapping("providerModify.html")
    public  ModelAndView  providerModify(String proid){
        Provider provider = providerService.providerView(proid);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("provider", provider);
        modelAndView.setViewName("providermodify");
        return modelAndView ;
    }
    //执行用户修改操作
     @RequestMapping("doProviderModify.html")
     public String doProviderModify( String proid , String proCode,String proName,String proContact,String proPhone,
                                  String proAddress, String proFax,String proDesc){
         Provider provider = new Provider();
         provider.setId(Integer.parseInt(proid));
         provider.setProCode(proCode);
         provider.setProName(proName);
         provider.setProContact(proContact);
         provider.setProPhone(proPhone);
         provider.setProAddress(proAddress);
         provider.setProDesc(proDesc);
         int affectRows = providerService.doProviderModify(provider);
         if (affectRows >0 ){
             return "redirect:/provider/provider.html";
         }else {
             return "redirect:/provider/providerModify.html";
         }

     }











}
